import React, { useEffect, useState } from 'react';
import ReactECharts from 'echarts-for-react';
import { useWindowSize } from '../hooks/useWindowSize';

const BarChart2: React.FC = () => {
  const { width } = useWindowSize();
  const [fontSize, setFontSize] = useState(16);
  const [chartHeight, setChartHeight] = useState('100%');

  useEffect(() => {
    if (width <= 480) {
      setFontSize(12);
      setChartHeight('200px');
    } else if (width <= 768) {
      setFontSize(14);
      setChartHeight('250px');
    } else if (width <= 992) {
      setFontSize(15);
      setChartHeight('300px');
    } else {
      setFontSize(16);
      setChartHeight('100%');
    }
  }, [width]);

  const option = {
    title: {
      text: '对比分析',
      textStyle: {
        color: '#fff',
        fontSize: fontSize
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      confine: true
    },
    legend: {
      data: ['2023', '2024'],
      textStyle: {
        color: '#fff',
        fontSize: fontSize - 2
      },
      top: fontSize + 5
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
      top: fontSize * 2 + 30
    },
    xAxis: {
      type: 'category',
      data: ['一月', '二月', '三月', '四月', '五月', '六月'],
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff',
        interval: width <= 480 ? 1 : 0
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)'
        }
      }
    },
    series: [
      {
        name: '2023',
        type: 'bar',
        data: [120, 132, 101, 134, 90, 230],
        barWidth: width <= 480 ? '30%' : '40%',
        itemStyle: {
          color: '#ee6666'
        }
      },
      {
        name: '2024',
        type: 'bar',
        data: [220, 182, 191, 234, 290, 330],
        barWidth: width <= 480 ? '30%' : '40%',
        itemStyle: {
          color: '#73c0de'
        }
      }
    ]
  };

  return (
    <ReactECharts
      option={option}
      style={{ height: chartHeight, width: '100%' }}
      opts={{ renderer: 'svg' }}
      notMerge={true}
      lazyUpdate={true}
    />
  );
};

export default BarChart2; 